﻿using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ModulesToolkit.DataContracts;
using ModulesToolkit.DataContracts.Membership;

namespace ModulesToolkit.DataAccess.Membership
{
    public class GroupRolesDtoRepository
    {
        #region Methods

        public EntitySearchResult<GroupRoleDto> RetrieveGroupRoles(GroupRolesDtoSearchCriteria searchCriteria)
        {
            Database database = DatabaseFactory.CreateDatabase();
            using (DbCommand command = database.GetStoredProcCommand("[Membership].[RetrieveGroupRoles]"))
            {
                try
                {
                    return new EntitySearchResult<GroupRoleDto>(
                        database.Execute<GroupRoleDto>(command, searchCriteria.AsParameters().ToArray()).ToList(),
                        database.GetParameterValue(command, "RETURN_VALUE").ConvertTo<int>().GetValueOrDefault()
                        );
                }
                catch (SqlException exception)
                {
                    throw exception.Translate();
                }
            }
        }


        #endregion Methods
    }
}
